package com.weakteam.androidclient.app.menu;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import com.weakteam.androidclient.app.R;
import com.weakteam.androidclient.data.ResourceData;
import com.weakteam.androidclient.data.domain.Menu;

import java.util.Iterator;
import java.util.List;

/**
 * @author Nail
 */
public class MenuView extends ScrollView {

    private Context context;

    public MenuView(Context context) {
        super(context);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.menu_view, this, true);

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;

        List<Menu> menuList = ResourceData.getInstance().getMenuList();

        int columnCount = 3;
        float borderMargin = 16.0f;
        float textHeight = 16.0f;
        float contentMargin = 32.0f;

        float buttonWidth = (width - 2.0f*borderMargin - (columnCount - 1)*contentMargin ) / columnCount;
        float buttonHeight = buttonWidth + textHeight;

        int i = 0;
        TableRow row = null;
        LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TableLayout menuTable = (TableLayout) findViewById(R.id.menu_table);

        for(Iterator<Menu> itMenu = menuList.iterator(); itMenu.hasNext();) {
            Menu menu = itMenu.next();

            if (i % columnCount == 0) {
                row = (TableRow)inflater.inflate(R.layout.menu_tablerow, null);
                menuTable.addView(row);
            }
            i++;

            MenuButton menuButton = new MenuButton(this.context);
            menuButton.setMenu(menu);
            row.addView(menuButton);
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);


    }


    public void setSize(float width) {
        int n = this.getChildCount();
        for (int i = 0; i < n; i++) {
            MenuButton menuButton = (MenuButton)this.getChildAt(i);
            menuButton.setSize(width / 3.0f);
        }
    }

}